<template>
  <el-dialog width="480px" v-model="showDialog" :close-on-click-modal="false" :modal-append-to-body="false" :close-on-press-escape="false">
    <template #header>
      <span>{{ mainTitle }}信息</span>
    </template>
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="110px">
      <el-form-item :label="'归属店铺'" prop="storeId" v-if="!orgStore.isStore">
        <store-select v-model:storeData="storeData" :default-value="ruleForm.storeId" class="w240 ml-12" />
      </el-form-item>
      <el-form-item :label="'打印机名称'" prop="customPrinterName">
        <el-input class="w240" v-model="ruleForm.customPrinterName" placeholder="请输入" />
      </el-form-item>
      <el-form-item :label="'打印用途'" prop="receiptType">
        <el-radio-group v-model="ruleForm.receiptType" class="ml-4" :disabled="isEdit">
          <el-radio :label="0" size="large">前台打印</el-radio>
          <el-radio :label="1" size="large">后厨打印</el-radio>
          <el-radio :label="2" size="large">传菜打印</el-radio>
          <el-radio :label="3" size="large">外卖打印</el-radio>
          <el-radio :label="4" size="large">小程序打印</el-radio>
        </el-radio-group>
      </el-form-item>
      <div v-if="ruleForm.receiptType === 0">
        <!-- <el-form-item :label="'档口名'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'端口类型'" prop="type">
          <el-radio-group v-model="ruleForm.type" class="ml-4">
            <el-radio :label="0" size="large">USB</el-radio>
            <el-radio :label="1" size="large">网口</el-radio>
            <el-radio :label="2" size="large">串口</el-radio>
            <el-radio :label="3" size="large">蓝牙</el-radio>
            <el-radio :label="4" size="large">驱动</el-radio>
            <el-radio :label="5" size="large">并口</el-radio>
            <el-radio :label="6" size="large">其他</el-radio>
            <el-radio :label="7" size="large">云打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'端口名称'" prop="port" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.port" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'pid'" prop="pid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.pid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'vid'" prop="vid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.vid" placeholder="请输入" />
        </el-form-item>
        <!-- （1：中崎 2：芯烨）") -->
        <el-form-item :label="'云打印机厂商'" prop="cloudCompany" v-if="ruleForm.type === 7">
          <el-select v-model="ruleForm.cloudCompany" class="w240" placeholder="请选择">
            <el-option :label="'中崎'" :value="1"></el-option>
            <el-option :label="'芯烨'" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'打印机通信id'" prop="uniqueId" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.uniqueId" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信key'" prop="pass" v-if="ruleForm.type === 7 && ruleForm.cloudCompany !== 2">
          <el-input class="w240" v-model="ruleForm.pass" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'ip地址'" prop="ip" v-if="ruleForm.type === 1">
          <el-input class="w240" v-model="ruleForm.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'波特率'" prop="baudRate" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.baudRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'蓝牙mac地址'" prop="macAddress" v-if="ruleForm.type === 3">
          <el-input class="w240" v-model="ruleForm.macAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'纸张大小'" prop="sizeType">
          <el-select v-model="ruleForm.sizeType" class="w240" placeholder="请选择">
            <el-option :label="'58mm'" :value="0"></el-option>
            <el-option :label="'80mm'" :value="1"></el-option>
            <el-option :label="'35*25mm'" :value="2"></el-option>
            <el-option :label="'40*30mm'" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="'小票模板'" prop="receiptId">
          <el-select v-model="ruleForm.receiptId" class="w240" placeholder="请选择">
            <el-option v-for="item in receiptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item :label="'小票类型'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'打印机蜂鸣提示'" prop="isBeep">
          <el-radio-group v-model="ruleForm.isBeep" class="ml-4">
            <el-radio :label="0" size="large">关闭</el-radio>
            <el-radio :label="1" size="large">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机状态'" prop="status">
          <el-radio-group v-model="ruleForm.status" class="ml-4">
            <el-radio :label="-1" size="large">停用</el-radio>
            <el-radio :label="1" size="large">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div v-if="ruleForm.receiptType === 1">
        <!-- <el-form-item :label="'档口名'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'取餐口标'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'端口类型'" prop="type">
          <el-radio-group v-model="ruleForm.type" class="ml-4">
            <el-radio :label="0" size="large">USB</el-radio>
            <el-radio :label="1" size="large">网口</el-radio>
            <el-radio :label="2" size="large">串口</el-radio>
            <el-radio :label="3" size="large">蓝牙</el-radio>
            <el-radio :label="4" size="large">驱动</el-radio>
            <el-radio :label="5" size="large">并口</el-radio>
            <el-radio :label="6" size="large">其他</el-radio>
            <el-radio :label="7" size="large">云打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'端口名称'" prop="port" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.port" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'pid'" prop="pid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.pid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'vid'" prop="vid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.vid" placeholder="请输入" />
        </el-form-item>
        <!-- （1：中崎 2：芯烨）") -->
        <el-form-item :label="'云打印机厂商'" prop="cloudCompany" v-if="ruleForm.type === 7">
          <el-select v-model="ruleForm.cloudCompany" class="w240" placeholder="请选择">
            <el-option :label="'中崎'" :value="1"></el-option>
            <el-option :label="'芯烨'" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'打印机通信id'" prop="uniqueId" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.uniqueId" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信key'" prop="pass" v-if="ruleForm.type === 7 && ruleForm.cloudCompany !== 2">
          <el-input class="w240" v-model="ruleForm.pass" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'ip地址'" prop="ip" v-if="ruleForm.type === 1">
          <el-input class="w240" v-model="ruleForm.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'波特率'" prop="baudRate" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.baudRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'蓝牙mac地址'" prop="macAddress" v-if="ruleForm.type === 3">
          <el-input class="w240" v-model="ruleForm.macAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'纸张大小'" prop="sizeType">
          <el-select v-model="ruleForm.sizeType" class="w240" placeholder="请选择">
            <el-option :label="'58mm'" :value="0"></el-option>
            <el-option :label="'80mm'" :value="1"></el-option>
            <el-option :label="'35*25mm'" :value="2"></el-option>
            <el-option :label="'40*30mm'" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label="'打印数'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'打印方式'" prop="printMode">
          <el-radio-group v-model="ruleForm.printMode" class="ml-4">
            <el-radio :label="0" size="large">整单打印</el-radio>
            <el-radio :label="1" size="large">单品分单</el-radio>
            <el-radio :label="2" size="large" v-if="ruleForm.printMode !== 0">单品分单且数量不为了1的商品换行</el-radio>
            <el-radio :label="3" size="large" v-if="ruleForm.printMode !== 0">单品分单且数量不为了1的商品分页打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机蜂鸣提示'" prop="isBeep">
          <el-radio-group v-model="ruleForm.isBeep" class="ml-4">
            <el-radio :label="0" size="large">关闭</el-radio>
            <el-radio :label="1" size="large">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机状态'" prop="status">
          <el-radio-group v-model="ruleForm.status" class="ml-4">
            <el-radio :label="-1" size="large">停用</el-radio>
            <el-radio :label="1" size="large">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div v-if="ruleForm.receiptType === 2">
        <!-- <el-form-item :label="'档口名'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'端口类型'" prop="type">
          <el-radio-group v-model="ruleForm.type" class="ml-4">
            <el-radio :label="0" size="large">USB</el-radio>
            <el-radio :label="1" size="large">网口</el-radio>
            <el-radio :label="2" size="large">串口</el-radio>
            <el-radio :label="3" size="large">蓝牙</el-radio>
            <el-radio :label="4" size="large">驱动</el-radio>
            <el-radio :label="5" size="large">并口</el-radio>
            <el-radio :label="6" size="large">其他</el-radio>
            <el-radio :label="7" size="large">云打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'端口名称'" prop="port" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.port" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'pid'" prop="pid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.pid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'vid'" prop="vid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.vid" placeholder="请输入" />
        </el-form-item>
        <!-- （1：中崎 2：芯烨）") -->
        <el-form-item :label="'云打印机厂商'" prop="cloudCompany" v-if="ruleForm.type === 7">
          <el-select v-model="ruleForm.cloudCompany" class="w240" placeholder="请选择">
            <el-option :label="'中崎'" :value="1"></el-option>
            <el-option :label="'芯烨'" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'打印机通信id'" prop="uniqueId" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.uniqueId" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信key'" prop="pass" v-if="ruleForm.type === 7 && ruleForm.cloudCompany !== 2">
          <el-input class="w240" v-model="ruleForm.pass" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'ip地址'" prop="ip" v-if="ruleForm.type === 1">
          <el-input class="w240" v-model="ruleForm.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'波特率'" prop="baudRate" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.baudRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'蓝牙mac地址'" prop="macAddress" v-if="ruleForm.type === 3">
          <el-input class="w240" v-model="ruleForm.macAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'纸张大小'" prop="sizeType">
          <el-select v-model="ruleForm.sizeType" class="w240" placeholder="请选择">
            <el-option :label="'58mm'" :value="0"></el-option>
            <el-option :label="'80mm'" :value="1"></el-option>
            <el-option :label="'35*25mm'" :value="2"></el-option>
            <el-option :label="'40*30mm'" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label="'打印数'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <!-- <el-form-item :label="'档口号'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'打印方式'" prop="printMode">
          <el-radio-group v-model="ruleForm.printMode" class="ml-4">
            <el-radio :label="0" size="large">整单打印</el-radio>
            <el-radio :label="1" size="large">单品分单</el-radio>
            <el-radio :label="2" size="large" v-if="ruleForm.printMode !== 0">单品分单且数量不为了1的商品换行</el-radio>
            <el-radio :label="3" size="large" v-if="ruleForm.printMode !== 0">单品分单且数量不为了1的商品分页打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机蜂鸣提示'" prop="isBeep">
          <el-radio-group v-model="ruleForm.isBeep" class="ml-4">
            <el-radio :label="0" size="large">关闭</el-radio>
            <el-radio :label="1" size="large">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机状态'" prop="status">
          <el-radio-group v-model="ruleForm.status" class="ml-4">
            <el-radio :label="-1" size="large">停用</el-radio>
            <el-radio :label="1" size="large">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div v-if="ruleForm.receiptType === 3">
        <!-- <el-form-item :label="'档口名'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'端口类型'" prop="type">
          <el-radio-group v-model="ruleForm.type" class="ml-4">
            <el-radio :label="0" size="large">USB</el-radio>
            <el-radio :label="1" size="large">网口</el-radio>
            <el-radio :label="2" size="large">串口</el-radio>
            <el-radio :label="3" size="large">蓝牙</el-radio>
            <el-radio :label="4" size="large">驱动</el-radio>
            <el-radio :label="5" size="large">并口</el-radio>
            <el-radio :label="6" size="large">其他</el-radio>
            <el-radio :label="7" size="large">云打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'端口名称'" prop="port" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.port" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'pid'" prop="pid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.pid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'vid'" prop="vid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.vid" placeholder="请输入" />
        </el-form-item>
        <!-- （1：中崎 2：芯烨）") -->
        <el-form-item :label="'云打印机厂商'" prop="cloudCompany" v-if="ruleForm.type === 7">
          <el-select v-model="ruleForm.cloudCompany" class="w240" placeholder="请选择">
            <el-option :label="'中崎'" :value="1"></el-option>
            <el-option :label="'芯烨'" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'打印机通信id'" prop="uniqueId" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.uniqueId" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信key'" prop="pass" v-if="ruleForm.type === 7 && ruleForm.cloudCompany !== 2">
          <el-input class="w240" v-model="ruleForm.pass" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'ip地址'" prop="ip" v-if="ruleForm.type === 1">
          <el-input class="w240" v-model="ruleForm.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'波特率'" prop="baudRate" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.baudRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'蓝牙mac地址'" prop="macAddress" v-if="ruleForm.type === 3">
          <el-input class="w240" v-model="ruleForm.macAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'纸张大小'" prop="sizeType">
          <el-select v-model="ruleForm.sizeType" class="w240" placeholder="请选择">
            <el-option :label="'58mm'" :value="0"></el-option>
            <el-option :label="'80mm'" :value="1"></el-option>
            <el-option :label="'35*25mm'" :value="2"></el-option>
            <el-option :label="'40*30mm'" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label="'小票类型'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'打印机蜂鸣提示'" prop="isBeep">
          <el-radio-group v-model="ruleForm.isBeep" class="ml-4">
            <el-radio :label="0" size="large">关闭</el-radio>
            <el-radio :label="1" size="large">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机状态'" prop="status">
          <el-radio-group v-model="ruleForm.status" class="ml-4">
            <el-radio :label="-1" size="large">停用</el-radio>
            <el-radio :label="1" size="large">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div v-if="ruleForm.receiptType === 4">
        <!-- <el-form-item :label="'档口名'" prop="pickUpPoint">
          <el-input class="w240" v-model="ruleForm.pickUpPoint" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'端口类型'" prop="type">
          <el-radio-group v-model="defaultType" class="ml-4" :disabled="true">
            <el-radio :label="0" size="large">USB</el-radio>
            <el-radio :label="1" size="large">网口</el-radio>
            <el-radio :label="2" size="large">串口</el-radio>
            <el-radio :label="3" size="large">蓝牙</el-radio>
            <el-radio :label="4" size="large">驱动</el-radio>
            <el-radio :label="5" size="large">并口</el-radio>
            <el-radio :label="6" size="large">其他</el-radio>
            <el-radio :label="7" size="large">云打印</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'端口名称'" prop="port" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.port" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'pid'" prop="pid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.pid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'vid'" prop="vid" v-if="ruleForm.type === 0">
          <el-input class="w240" v-model="ruleForm.vid" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信id'" prop="uniqueId" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.uniqueId" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'打印机通信key'" prop="pass" v-if="ruleForm.type === 7">
          <el-input class="w240" v-model="ruleForm.pass" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'ip地址'" prop="ip">
          <el-input class="w240" v-model="ruleForm.ip" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'波特率'" prop="baudRate" v-if="ruleForm.type === 2">
          <el-input class="w240" v-model="ruleForm.baudRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'蓝牙mac地址'" prop="macAddress" v-if="ruleForm.type === 3">
          <el-input class="w240" v-model="ruleForm.macAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="'纸张大小'" prop="sizeType">
          <el-select v-model="ruleForm.sizeType" class="w240" placeholder="请选择">
            <el-option :label="'58mm'" :value="0"></el-option>
            <el-option :label="'80mm'" :value="1"></el-option>
            <el-option :label="'35*25mm'" :value="2"></el-option>
            <el-option :label="'40*30mm'" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item :label="'小票模板'" prop="receiptId">
          <el-select v-model="ruleForm.receiptId" class="w240" placeholder="请选择">
            <el-option v-for="item in receiptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item> -->

        <!-- <el-form-item :label="'小票类型'" prop="name">
          <el-input class="w240" v-model="ruleForm.name" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item :label="'打印机蜂鸣提示'" prop="isBeep">
          <el-radio-group v-model="ruleForm.isBeep" class="ml-4">
            <el-radio :label="0" size="large">关闭</el-radio>
            <el-radio :label="1" size="large">开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="'打印机状态'" prop="status">
          <el-radio-group v-model="ruleForm.status" class="ml-4">
            <el-radio :label="-1" size="large">停用</el-radio>
            <el-radio :label="1" size="large">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button v-blur @click="showDialog = false">取消</el-button>
        <el-button v-blur type="primary" @click="submitForm(ruleFormRef)">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

import StoreSelect from '@/views/components/select-store/index.vue'

import { showReqError } from '@/hooks/useError'

import { useOrgStore } from '@/store/modules/org'

import { http } from '@/api'

const orgStore = useOrgStore()

/* const props = defineProps({
  mainTitle: {
    type: String,
    default: ''
  }
}) */
/**
 * 必要参数
 *  showDialog 是否显示弹框
 *  ruleFormRef 表单
 *  rules 配置代码
 *  dialogClosed 弹框关闭，父组件更新
 *  submitForm 校验提交表单
 *  isEdit 是否是编辑
 */
let receiptOptions = ref([])
let isEdit = false
let ruleForm = ref({
  name: ''
})
let storeData = ref()

// 小程序默认选择值
let defaultType = ref(1)
// let timePicker = ref([])
/**
 * 父组件调弹框显示方法
 */
const show = (row) => {
  findReceiptFromByList()
  showDialog.value = true
  ruleForm.value = {}
  // 修改时
  if (row) {
    details(row)
    isEdit = true
    return
  }
  // 新增时
  // 默认选中
  ruleForm.value.isBeep = 0
  ruleForm.value.status = 1
  isEdit = false
}

/**
 * 获取小票模板
 */
const findReceiptFromByList = async () => {
  const params = {
    type: 0 // 打印类型：（0：前台，1：后厨，2：中传）
  }
  // console.log(666666666, params)
  try {
    let res = await http('findReceiptFromByList', params)
    receiptOptions.value = res.content
    console.log(res)
  } catch (error) {
    console.log(error)
  }
}

/**
 * 处理请求数据
 */
const handleSubmitData = async () => {
  const apiName = !isEdit ? 'printerAdd' : 'printerModify'
  let params = {
    ...ruleForm.value,
    insertPrinterRelReceipt: [{ receiptId: ruleForm.value.receiptId }]
  }
  // console.log(storeData.value, 'storeData')
  if (storeData.value) {
    params.storeId = storeData.value.id
    params.storeName = storeData.value.name
  }

  // ruleForm.receiptId
  /* ruleForm.value.insertPrinterRelReceipt = [
    {
      receiptId: ruleForm.value.receiptId
    }
  ] */

  console.log(params, 'params')
  // params.storeId = storeId.value[storeId.value.length - 1]
  try {
    let res = await http(apiName, params)
    if (res.isError) return showReqError(res)
    ElMessage.success('保存成功')
    showDialog.value = false
    dialogClosed()
  } catch (error) {
    console.log(error)
  }
}

const showDialog = ref(false)
const ruleFormRef = ref()

const rules = reactive({
  customPrinterName: [{ required: true, message: `请输入`, trigger: 'blur' }],
  receiptType: [{ required: true, message: `请选择`, trigger: 'blur' }],
  type: [{ required: true, message: `请选择`, trigger: 'blur' }],
  sizeType: [{ required: true, message: `请选择`, trigger: 'blur' }],
  name: [{ required: true, message: `请输入`, trigger: 'blur' }],
  macAddress: [{ required: true, message: `请输入`, trigger: 'blur' }],
  receiptId: [{ required: true, message: `请选择`, trigger: 'blur' }]
})

const emit = defineEmits(['dialogClosed'])
const dialogClosed = () => {
  emit('dialogClosed')
}
const details = async (row) => {
  let params = {
    id: row.id
  }
  try {
    let res = await http('printerDetails', params)
    ruleForm.value = { ...res.content, id: row.id }

    // ruleForm.value.receiptId = ruleForm.value.printerReceiptFrom.map((item) => item.id)
    ruleForm.value.receiptId = ruleForm.value.printerReceiptFrom[0].id

    if (res.isError) return showReqError(res)
  } catch (error) {
    console.log(error)
  }
}
const submitForm = async (formEl) => {
  if (ruleForm.value.receiptType === 4) {
    ruleForm.value.type = 1
  }

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      handleSubmitData()
    } else {
      console.log('error submit!', fields)
    }
  })
}
defineExpose({ show })
</script>
<style lang="scss" scoped></style>
